﻿using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Model;
using System.Data;
using Smobiler.Device;
using System.Web.Script.Serialization;
using System.Threading;

namespace FubiManger.Kitchen
{
    partial class KitchenForm : Smobiler.Core.Controls.MobileForm
    {
        public KitchenForm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        /// <summary>
        /// 商家ID
        /// </summary>
        public string merchantid { get; set; }
        /// <summary>
        /// 商品集合
        /// </summary>
        List<Gdpro> prolist = new List<Gdpro>();
        /// <summary>
        /// 初始化订单
        /// </summary>
        private ms_order order = new ms_order();
        private void KitchenForm_Load(object sender, EventArgs e)
        {
           // merchantid = "2";
            this.Client.HeartBeatEnable(6000);            
            Getorder();
            //timer1.Start();
        }
        /// <summary>
        /// 获取今日订单
        /// </summary>
        private void Getorder()
        {
            if (lvord.Rows.Count > 0)
                lvord.Rows.Clear();
            List<ms_order> ordlist = orderDal.Querybytoday(merchantid);
            DataTable table = new DataTable();
            table.Columns.Add("ordsn");
            table.Columns.Add("disksn");
            table.Columns.Add("time");
            for (int i = 0; i < ordlist.Count; i++)
            {
                table.Rows.Add(ordlist[i].order_sn, "位置：" + ordlist[i].orderdisksn, ordlist[i].add_time);
            }
            lvord.DataSource = table;
            lvord.DataBind();
        }

        /// <summary>
        /// 获取打印订单内容
        /// </summary>
        /// <param name="ordsn"></param>
        public void Getprinttxt(string ordsn)
        {
           // orderDal.Updateprint(ordsn);
            order = orderDal.Querybysn(ordsn);
            JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
            prolist = js.Deserialize<List<Gdpro>>(order.goods_info);
            List<Printlist> plist = new List<Printlist>();
            //for (int i = 0; i < prolist.Count; i++)
            //{
            //    Printlist pt = new Printlist();
            //    string title = "TEXT 5," + 0 + ",\"TSS16.BF2\",0,1,1,\"" + "位置：" + order.orderdisksn + "\"\r\n";
            //    string str1 = "TEXT 5," + 35 + ",\"TSS16.BF2\",0,1,1,\"" + "----------------------------------" + "\"\r\n";
            //    string str2 = "TEXT 5," + 65 + ",\"TSS16.BF2\",0,1,1,\"" + prolist[i].proname + "     x" + prolist[i].count + "\"\r\n";
            //    string str3 = "TEXT 5," + 95 + ",\"TSS16.BF2\",0,1,1,\"" + "----------------------------------" + "\"\r\n";
            //    string str4 = title + str1 + str2 + str3;
            //    pt.printxt = str4;
            //    plist.Add(pt);
            //}
            for (int i = 0; i < prolist.Count; i++)
            {
                Printlist pt = new Printlist();
                string title = "位置：" + order.consignee_address + "\r\n";
                string str1 = "------------------------\r\n";
                string str2 = prolist[i].proname + "     x" + prolist[i].count + "\r\n";
                string str3 = "------------------------\r\n";
                string str4 = title + str1 + str2 + str3;
                pt.printxt = str4;
                plist.Add(pt);
            }
            //string st1 = "位置：" + order.consignee_address + "\r\n";
            //string st2 = "------------------------\r\n";
            //string printxt = st1+st2;
            //for (int i = 0; i < prolist.Count; i++)
            //{
            //    string st3 = prolist[i].proname + "     x" + prolist[i].count + "\r\n";
            //    printxt += st3;
            //}
            //Print(printxt);
            for (int i = 0; i < plist.Count; i++)
            {
                //Thread th = new Thread(new ParameterizedThreadStart(Print));
                //th.Start(plist[i].printxt);
                Print(plist[i].printxt);
            }

        }
        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="printxt"></param>
        private void Print(Object printxt)
        {
            //string ptstr = printxt.ToString();
            //P390PrinterEntityCollection Commands1 = new P390PrinterEntityCollection();
            //Commands1.Add(new P390PrinterRawProtocolEntity("SIZE 57 mm, 15 mm\r\n" + "REFERENCE 0,0\r\n" + "SPEED 4.0\r\n" + "DENSITY 8\r\n" + "SET PEEL OFF\r\n" + "SET CUTTER OFF\r\n" + "SET TEAR ON\r\n" + "DIRECTION 0\r\n" + "SHIFT 0\r\n" + "OFFSET 0 mm\r\n" + "CLS\r\n" + ptstr + "PRINT 1,1\r\n"));

            //p390Printer1.Print(Commands1, (obj, args) =>
            //{
            //    if (args.isError == true)
            //        this.Toast("Error: " + args.error);
            //    else
            //    {
                   
            //    }
            //});
            P800PrintEntityCollection entityblueslection = new P800PrintEntityCollection();

            //文本打印
            P800PrintTextEntity text = new P800PrintTextEntity()
            {
                Content = printxt.ToString(),
                Offset = P800PrintOffSet.Left,
                IsBold = false,
                CharacterMultipleX = 1,
                CharacterMultipleY = 1,
                IsUnderLine = false,

            };
            entityblueslection.Add(text);
            //第一个参数：打印指令集，第二个参数：是否切纸，第三个参数：回调
            p800Printer1.Print(entityblueslection, true, (obj, args) =>
            {
                if (args.isError == true)
                    this.MessageBox.Show("Error: " + args.error);
                else
                    this.MessageBox.Show("success");

            });
        }
        int timer = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer++;
            if (timer == 6)
            {
                timer = 0;
                order = orderDal.QueryPrint(merchantid);
                if (!string.IsNullOrEmpty(order.order_sn))
                {
                    JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
                    prolist = js.Deserialize<List<Gdpro>>(order.goods_info);
                    List<Printlist> plist = new List<Printlist>();
                    //for (int i = 0; i < prolist.Count; i++)
                    //{
                    //    Printlist pt = new Printlist();
                    //    string title = "TEXT 5," + 0 + ",\"TSS16.BF2\",0,1,1,\"" + "位置：" + order.orderdisksn + "\"\r\n";
                    //    string str1 = "TEXT 5," + 35 + ",\"TSS16.BF2\",0,1,1,\"" + "----------------------------------" + "\"\r\n";
                    //    string str2 = "TEXT 5," + 65 + ",\"TSS16.BF2\",0,1,1,\"" + prolist[i].proname + "     x" + prolist[i].count + "\"\r\n";
                    //    string str3 = "TEXT 5," + 95 + ",\"TSS16.BF2\",0,1,1,\"" + "----------------------------------" + "\"\r\n";
                    //    string str4 = title + str1 + str2 + str3;
                    //    pt.printxt = str4;
                    //    plist.Add(pt);
                    //}
                    string st1 = "位置：" + order.orderdisksn + "\r\n";
                    string st2 = "------------------------\r\n";
                    string printxt = st1 + st2;
                    for (int i = 0; i < prolist.Count; i++)
                    {
                        string st3 = prolist[i].proname + "     x" + prolist[i].count + "\r\n";
                        printxt += st3;
                    }
                    Print(printxt);
                    for (int i = 0; i < prolist.Count; i++)
                    {
                        Printlist pt = new Printlist();
                        //string title = "位置："+ order.orderdisksn+"\r\n";
                        string str1 = "------------------------\r\n";
                        string str2 = prolist[i].proname + "     x" + prolist[i].count + "\r\n";
                        string str3 = "------------------------\r\n";
                        string str4 =  str1 + str2 + str3;
                        pt.printxt = str4;
                        plist.Add(pt);
                    }
                    for (int i = 0; i < plist.Count; i++)
                    {
                        //Thread th = new Thread(new ParameterizedThreadStart(Print));
                        //th.Start(plist[i].printxt);
                        Print(plist[i].printxt);
                    }
                    orderDal.Updateprint(order.order_sn);
                    Getorder();//获取订单
                }
            }
        }
        /// <summary>
        /// 关闭应用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void KitchenForm_KeyDown(object sender, KeyDownEventArgs e)
        {
            if (e.KeyCode == KeyCode.Back)
                this.Client.Exit();
        }

        private void button1_Press(object sender, EventArgs e)
        {
            
           
        }
    }
}
public class Printlist
{
    /// <summary>
    /// 打印内容
    /// </summary>
    public string printxt { get; set; }
}